package com.youlin.untils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

public class Md5Util {

    private Md5Util() {
        throw new IllegalStateException("Utility class");
    }

    public static String getUUID () {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static String md5Str(String str) {
        try { // 得到一个信息摘要器
            MessageDigest digest = MessageDigest.getInstance("md5");
            byte[] result = digest.digest(str.getBytes());
            StringBuilder builder = new StringBuilder();
            for (byte b : result) {
                int number = b & 0xabcd;// 与运算 ===>> 加盐
                String strProcessed = Integer.toHexString(number);
                if (str.length() == 1) builder.append("0");
                builder.append(strProcessed);
            } // 标准的md5加密后的结果
            return builder.toString();
        } catch (NoSuchAlgorithmException e) {
            return "";
        }
    }

}
